<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

include('includes/Admin.Core.php');

CheckAccess('managenews');


PrintHeader('常态内容');

$action = ForceIncomingString('action', 'displaycontents');
if(IsPost('deletecontents')) $action= 'deletecontents';

// ############################## FUNCTIONS #############################

function GetSearchSql($search) {
	$sWhere = '';
	if($search != ''){
		$sWhere .= " WHERE (username LIKE '%".$search."%' OR keywords LIKE '%".$search."%' OR title LIKE '%".$search."%' OR content LIKE '%".$search."%') ";
	}

	return $sWhere;
}

// ################################ INSERT content ################################

if($action == 'insertcontent' OR $action == 'updatecontent')
{
	$contentid     = ForceIncomingInt('contentid');
	$username        = ForceIncomingString('username', $userinfo['username']);
	$title        = ForceIncomingString('title');
	$content        = ForceIncomingString('content');
	$keywords        = ForceIncomingString('keywords');
	$deletecontent     = ForceIncomingInt('deletecontent');

	if ($deletecontent)
	{
		if($contentid != 1 AND $contentid != 2){
			$DB->query("DELETE FROM " . TABLE_PREFIX . "contents WHERE contentid = '$contentid' ");
		}
		PrintRedirect('admin.contents.php', 1);
	}

	if (strlen($title) == 0)
	{
		$errors[] = "常态内容标题不能为空!";
	}

	if(strlen($content) == 0){
		$errors[] = "常态内容不能为空!";
	}

	if ($action == 'insertcontent' AND !isset($errors))
	{
		$DB->query("INSERT INTO " . TABLE_PREFIX . "contents VALUES (NULL, '$username', '$title', '$content', '$keywords', '".time()."')");

		PrintRedirect('admin.contents.php', 1);

	}elseif($action == 'updatecontent' AND !isset($errors)){
		$DB->query("UPDATE " . TABLE_PREFIX . "contents SET 
		username = '$username',
		title     = '$title',
		content     = '$content',
		keywords     = '$keywords'
		WHERE contentid   = '$contentid'");

		PrintRedirect('admin.contents.php', 1);
	}else{
		PrintErrors($errors, Iif($contentid, '编辑常态内容错误', '添加常态内容错误'));
		$action = 'addcontent';
	}

}


// ############################### DELETE contentS ################################

if($action == 'deletecontents')
{
	$deletecontentids = ForceIncomingArray('deletecontentids');

	for($i = 0; $i < count($deletecontentids); $i++){
		if($deletecontentids[$i] != '1' AND $deletecontentids[$i] != '2'){
			$DB->query("DELETE FROM " . TABLE_PREFIX . "contents WHERE contentid = '$deletecontentids[$i]' ");
		}
	}

	PrintRedirect('admin.contents.php', 1);
}

// ############################ DISPLAY content FORUM #############################

if($action == 'addcontent' OR $action == 'editcontent')
{
	$contentid = ForceIncomingInt('contentid');

	if(isset($errors))
	{
		// content content or edit content with errors, redisplaying the form
		$content = array('contentid'   => $contentid,
			  'username'     => $username,
			  'title'     => $title,
			  'content'     => $_POST['content'],
			  'keywords'     => $keywords);

	} elseif($contentid) {
		$content = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "contents WHERE contentid = '$contentid'");
	}else{
		$content = array('contentid' => 0, 'username' => $userinfo['username']);
	}

	if($contentid)
	{
		PrintSection('编辑常态内容: ' . ShortTitle($content['title'], 36));
	}else{
		PrintSection('添加常态内容');
	}

	echo '<script src="'.BASEURL.'library/editor/nicEdit.js" type="text/javascript"></script>
	<script type="text/javascript">
	bkLib.onDomLoaded(function() {
		new nicEditor({fullPanel:true, iconsPath: \''.BASEURL.'library/editor/nicEditorIcons.gif\', maxHeight:400}).panelInstance("content");
	});
	</script>';

	echo '<form method="post" action="admin.contents.php">
	<input type="hidden" name="action" value="' . Iif($contentid, 'updatecontent', 'insertcontent') . '" />
	<input type="hidden" name="contentid" value="' . $content['contentid'] . '" />
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>发表人:</b></td>
	<td class="tdrow3"><input type="text" name="username" value="'.$content['username'].'" size="16" /></td>
	</tr>
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>常态内容标题:</b></td>
	<td class="tdrow3"><input type="text" name="title" value="'.$content['title'].'" size="40" /></td>
	</tr>
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>常态内容内容:</b></td>
	<td class="tdrow3" style="background: #fff;"><textarea name="content" style="height:390px;width:600px;" id="content">'.$content['content'].'</textarea></td>
	</tr>	
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>Meta关键字:</b></td>
	<td class="tdrow3"><input type="text" name="keywords" value="'.$content['keywords'].'" size="60" /></td>
	</tr>';

	if($contentid)
	{
		echo '<tr>
		<td class="tdrow2" nowrap="nowrap"><b>是否删除:</b></td>
		<td class="tdrow3"><input type="checkbox" name="deletecontent" value="1"> <b>是:</b> <span class=ohred>是否删除此常态内容?</span></td>
		</tr>';
	}

	echo '<tr>
	<td class="tdrow1" bgcolor="#FCFCFC" colspan="2" align="center">
	<input type="submit" value="' . Iif($contentid, '保存更新', '提交保存') . '" />
	</td>
	</tr>
	</table>
	</form>';

	EndSection();
}


// ############################### DISPLAY contentS ###############################

if($action == 'displaycontents')
{
	$NumPerPage =20;

	$page = ForceIncomingInt('page', 1);
	$search = ForceIncomingString('s');

	if(IsGet('s')){
		$search = urldecode($search);
	}

	$start = $NumPerPage * ($page-1);

	PrintSection('调用常态内容');
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" colspan="3">常态内容调用说明:</td>
	</tr>
	<tr>
	<td class="tdrow3" colspan="3">
	<b>方法1</b>: 参阅about.php和about.tpl两个文件调用常态内容;
	<BR><b>方法2</b>: 在前台模板文件中直接调用常态内容的Smarty代码如下: 
	<BR>首先分配变量(ID指常态内容的ID号):&nbsp;&nbsp;&nbsp;&nbsp;{assign var="mycontent" value=\'ID\'|GetContent} 
	<BR>调用常态内容的标题:&nbsp;&nbsp;&nbsp;&nbsp;{$mycontent.title}
	<BR>调用常态内容的主体:&nbsp;&nbsp;&nbsp;&nbsp;{$mycontent.content|html}
	<BR>调用常态内容的作者:&nbsp;&nbsp;&nbsp;&nbsp;{$mycontent.username}
	<BR>调用常态内容的日期:&nbsp;&nbsp;&nbsp;&nbsp;{$mycontent.created|DisplayDate:\'Y-m-d\'}
	</td>
	</tr>
	</table>';
	EndSection();

	PrintSection('搜索常态内容');
	echo '<form method="post" action="admin.contents.php" name="searchcontents">
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow2" align="center">搜索:&nbsp;<input type="text" name="s" size="22">&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="searchcontent" value="搜索常态内容" /></td>
	</tr>
	</table></form>';
	EndSection();

	$title = Iif(IsPost('searchcontent'), '搜索到的常态内容列表', '全部常态内容列表');

	$getcontents = $DB->query("SELECT contentid, username, title, created FROM " . TABLE_PREFIX . "contents ".GetSearchSql($search)." ORDER BY contentid DESC LIMIT $start,$NumPerPage");

	$maxrows = $DB->query_first("SELECT COUNT(contentid) AS value FROM " . TABLE_PREFIX . "contents ".GetSearchSql($search));

	echo '&nbsp;&nbsp;&nbsp;&nbsp;<a href="admin.contents.php?action=addcontent"><img src="'.TEMPLATEPATH.'images/open.gif" align="absmiddle"> <font class=ohblueb><u>添加常态内容</u></font></a>';

	PrintSection($title.'('.$maxrows['value'].'个)');

	echo '<form method="post" action="admin.contents.php" name="contentsform">
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" nowrap="nowrap">ID号</td>
	<td class="tdrow1" nowrap="nowrap">标题</td>
	<td class="tdrow1" nowrap="nowrap">发表人</td>
	<td class="tdrow1" nowrap="nowrap">日期</td>
	<td class="tdrow1" nowrap="nowrap"><input type="checkbox" checkall="group" onclick="select_deselectAll (\'contentsform\', this, \'group\');"> 删除</td>
	</tr>';

	if($maxrows['value'] < 1){
		echo '<tr><td class="tdrow3" colspan="10" align="center" height="48" valign="middle"><font class=ohredb>未搜索到任何常态内容!</font></td></tr>';
	}else{
		while($content = $DB->getrow($getcontents))
		{
			echo '<tr>
			<td class="tdrow2">'.$content['contentid'].'</td>
			<td class="tdrow3"><a href="admin.contents.php?action=editcontent&contentid='.$content['contentid'].'"  title="'.$content['title'].'">'.ShortTitle($content['title'], 48).'</a></td>
			<td class="tdrow2">' . $content['username'] . '</td>
			<td class="tdrow3" nowrap="nowrap">' . DisplayDate($content['created'], 'Y-m-d') . '</td>
			<td class="tdrow2"><input type="checkbox" name="deletecontentids[]" value="' . $content['contentid'] . '" checkme="group" /></td>
			</tr>';
		}
	}

	$totalpages = ceil($maxrows['value'] / $NumPerPage);

	if($totalpages > 1){
		echo '<tr><td class="tdrow3" colspan="7" align="center" height="48" valign="middle">';
		echo GetPageList('admin.contents.php', $totalpages, $page, 10, 's', urlencode($search));
		echo '</td></tr>';
	}

	if($maxrows['value'] > 0){
		echo '<tr><td class="tdrow1" colspan="7" align="center"><input type="submit" name="deletecontents" onclick="return confirm(\'确定删除所选常态内容吗?\');" value=" 删除常态内容 " /></td></tr>';
	}

	echo '</table></form>';

	EndSection();
}


// ############################### PRINT FOOTER ################################

PrintFooter();

?>